package com.jamila.scancode.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jamila.scancode.entity.BasicInfo;
import com.jamila.scancode.mapper.BasicInfoMapper;
import com.jamila.scancode.service.BasicInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author JAMILA
 * @since 2024-03-30 01:42:05
 */
@Service
public class BasicInfoServiceImpl extends ServiceImpl<BasicInfoMapper, BasicInfo> implements BasicInfoService {

    @Override
    public boolean saveData(BasicInfo basicInfo) {
        //根据手机号判断是否已存在基本信息；若存在则执行更新操作；若不存在则执行保存操作
        String tel = basicInfo.getTel();
        if (!StringUtils.hasLength(tel)){
            return false;
        }
        QueryWrapper<BasicInfo> wrapper=new QueryWrapper<>();
        wrapper.eq("tel",tel);
        BasicInfo basicInfo1 = baseMapper.selectOne(wrapper);
        if (basicInfo1==null){
            //执行保存操作
            int insert = baseMapper.insert(basicInfo);
            return insert>0;
        }else {
            //执行更新操作
            int update = baseMapper.updateById(basicInfo);
            return update>0;
        }

    }

    @Override
    public BasicInfo getBasicInfo(String tel) {
        QueryWrapper<BasicInfo> wrapper=new QueryWrapper<>();
        wrapper.eq("tel",tel);
        BasicInfo basicInfo = baseMapper.selectOne(wrapper);
        return basicInfo;
    }
}
